﻿
<template>
    <div>
        <!-- 添加按钮 -->

        <el-row :gutter="20" style="padding:20px;height:60px" v-show="plbtns.length>0">
            <el-col :span="24" style="text-align:right">
                <el-button v-for="czcol in plbtns" @click="plmangcol(czcol)" size="medium" :type="czcol.bttype">{{czcol.czname}}</el-button>
            </el-col>
        </el-row>
        <component class="widget" :is="zydwiget" :pzoption="zydquery"></component>
        <!-- 按条件选择 -->

        <div class="tab-filter-type mt10" v-show="jtcxdata.length>0">
            <div class="oh " v-for="item in jtcxdata">
                <h5 class="pull-left tr">{{item.cxname}}：</h5>
                <ul class="tab-type ft14">
                    <li v-for="lb in item.lbdata"><span v-bind:class="{ 'active': lb.issel }" @click="sellb(lb,item)" v-text="lb.itemtext"></span></li>
                </ul>
            </div>
        </div>
        <!-- 表格 -->
        <div class="default-tab ft14 padding20 hover-btn" style="padding-top: 10px;">
            <base-table :pzoption="tableop" @selection-change="handleSelectionChange" ref="table">
            </base-table>
        </div>


    </div>
</template>

<script>
    module.exports = {
        props: ['pdata'],
        components: {
            'base-table': httpVueLoader('/ViewV5/Base/Vue/Table.vue')
        },
        data: function () {
            return {
                zydwiget: "",
                plbtns: [],
                MXCZData: [],
                syscz: [{ czname: "新增", isshow: false }, { czname: "批量删除", isshow: false }, { czname: "编辑", isshow: false }, { czname: "删除", isshow: false }],
                ActionData: [],
                mrcxdata: [],
                zydquery: { value: [] },
                tableop: {
                    title: "自定义应用",
                    tablename: "",
                    ispage: true,//后台分页
                    pageoldindex: 1,
                    dtcxdata: [],//默认动态查询
                    pageindex: 1,
                    pagesize: 200,
                    pageallcount: 0,
                    issel: true,
                    iscz: true,
                    isadd: true,
                    isxh: true,
                    ishj: false,
                    isedit: true,
                    loading: true,
                    ispldel: true,
                    isdel: true,                   
                    dataset: [],
                    pdid: "0",
                    multipleSelection: [],
                    czltabledata: [

                    ],
                    collist: [

                    ]
                }
            }
        },
        computed: {
            dtcxdata: function () {
                var chi = this;
                return _.filter(chi.mrcxdata, function (col) {
                    return col.datatype == "1";
                })
            },
            jtcxdata: function () {
                var chi = this;
                return _.filter(chi.mrcxdata, function (col) {
                    return col.datatype != "1";
                })
            },
        },
        methods: {
       
            ishasqx: function () {

            },
            plmangcol: function (coldata) {
                var chi = this;
                if (coldata.czname == "新增") {
                    ComFunJS.winviewform("/ViewV5/AppPage/FORMBI/FormAdd.html?pdid=" + chi.tableop.pdid + "&vtype=2", "添加表单", "1000", "", function () {
                        chi.Query();

                    });

                } else if (coldata.czname == "批量删除") {
                    this.DelMItemData();
                } else {
                    try {
                        var ids = [];
                        for (var i = 0; i < this.tableop.multipleSelection.length; i++) {
                            ids.push(this.tableop.multipleSelection[i].ID || this.tableop.multipleSelection[i].idn);
                        }
                        let jscode = coldata.jscode;
                        let func = new Function('ids', jscode);
                        func(ids)
                    } catch (e) {
                        this.$notify({
                            title: '成功',
                            message: '解析JS代码有误',
                            type: 'error'
                        });
                    }
                }


            },
            handleSelectionChange(val) {
                this.tableop.multipleSelection = val;
            },
            DelItemData: function (item) {
                var pdid = this.tableop.pdid;
                this.DelData(item.intProcessStanceid, item.ID || item.idn)
            },
            DelMItemData: function () {
                var ids = [];
                var dataids = [];

                for (var i = 0; i < this.tableop.multipleSelection.length; i++) {
                    try {
                        ids.push(this.tableop.multipleSelection[i].intProcessStanceid);
                        dataids.push(this.tableop.multipleSelection[i].ID || this.tableop.multipleSelection[i].idn);
                    } catch (e) {

                    }
                }
                if (ids.length == 0) {
                    this.$notify.error({
                        title: '错误',
                        message: '至少需要选择一条数据才能操作'
                    });
                    return;
                }
                this.DelData(ids.join(), dataids.join());
            },
            DelData: function (ids, dataids) {
                var pro = this;
                var tablepar = JSON.parse(pro.$root.pagedata.ExtData);
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.getJSON('/api/Bll/ExeAction?Action=FORMBI_DELWFDATA', { "P2": ids, "P1": dataids, tabledata: tablepar.value.join(), pdid: pro.tableop.pdid  }, function (result) {
                        if (result.ErrorMsg == "") {
                            ComFunJS.winsuccess("删除成功");
                            pro.Query();

                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            sellb: function (lb, lbs) {
                _.forEach(lbs.lbdata, function (obj) {
                    obj.issel = false;
                })
                lb.issel = true;
                this.Query();
            },
            Query: function () {
                var pro = this;
                if (pro.tableop.pageoldindex == pro.tableop.pageindex) {
                    pro.tableop.pageindex = 1;
                }
                pro.tableop.loading = true;
                var tablepar = JSON.parse(pro.$root.pagedata.ExtData);
                var querydata = [];

                //处理静态查询数据
                _.forEach(pro.jtcxdata, function (obj) {
                    var va = _.find(obj.lbdata, function (cx) {
                        return cx.issel == true;
                    })
                    if (va.itemtext != "全部") {
                        querydata.push({ colid: obj.cxzd, cal: obj.cal, val: va.itemtext });
                    }
                })
                //处理静态查询数据

                //处理动态查询数据,去数据库找数据哦
                var dtwd = "";
                _.forEach(pro.dtcxdata, function (obj) {
                    if (obj.cxzd) {
                        dtwd = dtwd + obj.cxzd + ",";
                        if (obj.val != "全部" && obj.val != "") {
                            querydata.push({ colid: obj.cxzd, cal: obj.cal, val: obj.val });
                        }
                    }
                })
                //处理动态查询数据


                //处理自定义查询条件
                _.forEach(pro.zydquery.value, function (obj) {
                    obj.cxtype = "zdy";
                    querydata.push(obj);
                })
                //处理自定义查询条件

                //处理自身查询
                if (!_.isUndefined(pro.$refs.table)) {
                 
                    _.forEach(pro.$refs.table.tabquerydata, function (obj) {
                        querydata.push(obj);
                    })
                    if (pro.$refs.table.seastr) {
                        querydata.push({ colid: pro.$refs.table.sear.colid, cal: "4", val: pro.$refs.table.seastr });

                    }
                }//处理自身查询


                $.getJSON('/api/Bll/ExeAction?Action=DATABI_GETVUELISTDATA', { P1: tablepar.value.join(), P2: tablepar.dataqx ? "Y" : "N", WD: JSON.stringify(tablepar.WDData), qdata: JSON.stringify(querydata), pagesize: pro.tableop.pagesize, pageindex: pro.tableop.pageindex, ispage: pro.tableop.ispage, dtwd: dtwd, glbd: tablepar.glbd }, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        pro.tableop.dataset = resultData.Result2;
                        pro.tableop.pdid = resultData.Result3 ;
                        pro.tableop.pageallcount = resultData.DataLength;
                        pro.tableop.loading = false;
                        pro.tableop.pageoldindex = pro.tableop.pageindex;//为了解决切换查询页码显示得问题.
                        if (resultData.Result4) {
                            //如果有动态查询
                            _.forEach(resultData.Result4, function (item) {
                                _.forEach(pro.dtcxdata, function (obj) {
                                    if (item.cxzd == obj.cxzd) {
                                        obj.dtsource = item.dtsource;
                                    }
                                })
                            })

                        }

                    }
                })

            },
            InitWigetData: function () {
                var pro = this;
                var tablepar = JSON.parse(pro.$root.pagedata.ExtData);
                var allcz = tablepar.ActionData;// 操作列表
                var sqdata = pro.$root.pagedata.ActionData.split(',');//授权列表
                pro.tableop.title = tablepar.name;
                if (pro.tableop.collist.length == 0) {
                    pro.tableop.collist = [];
                    //排序
                    var tempwd = _.sortBy(tablepar.WDData, function (o) { return o.colorder; });
                    for (var i = 0; i < tempwd.length; i++) {
                        if (tempwd[i].colname) {
                            pro.tableop.collist.push({ colid: tempwd[i].colid, colname: tempwd[i].colname, isshow: tempwd[i].isshow, istip: true, ishj: tempwd[i].ishj });
                        }
                        if (tempwd[i].ishj) {
                            pro.tableop.ishj = true;
                        }
                    }
                };
                pro.plbtns = _.filter(allcz, function (item) { return item.isrowcz == '0' && _.findIndex(sqdata, function (ac) { return ac == item.czname }) > -1 });
                var rczbtns = _.filter(allcz, function (item) { return item.isrowcz == '1' });
                pro.tableop.czltabledata = [];
                _.forEach(rczbtns, function (mxcz) {
                    if (_.findIndex(sqdata, function (ac) { return ac == mxcz.czname }) > -1) {
                        if (mxcz.czname == "编辑") {
                           
                            pro.tableop.czltabledata.push({
                                colname: "编辑", bttype: "text", isshow: true, mangefun: function (rowdata) {
                                    var purl = "";
                                    try {
                                        var id = rowdata.ID || rowdata.idn;
                                        var pdid = pro.tableop.pdid;
                                        var purl = "&id=" + id + "&pdid=" + pdid;
                                    } catch (e) {

                                    }
                                    ComFunJS.winviewform("/ViewV5/AppPage/FORMBI/FormAdmin.html?piid=" + rowdata.intProcessStanceid + "&vtype=2" + purl, "管理表单", "1000", "", function () {
                                        pro.Query();
                                    });
                                },
                            });
                        } else if (mxcz.czname == "删除") {
                            pro.tableop.czltabledata.push({
                                colname: "删除", bttype: "text", isshow: true, mangefun: function (rowdata) {
                                    this.$parent.DelItemData(rowdata);
                                }
                            });
                        } else {
                            pro.tableop.czltabledata.push({
                                colname: mxcz.czname, bttype: mxcz.bttype, isshow: true, mangefun: function (rowdata) {
                                    if (mxcz.jscode) {
                                        let jscode = mxcz.jscode;
                                        let func = new Function('rowdata', jscode);
                                        func(rowdata)
                                    }
                                },
                            });
                        }

                    }
                });
                try {
                    //处理自定义查询组件,不需要查询
                    var isinitQ = false;
                    if (tablepar.zdydata && tablepar.zdydata.length > 0) {
                        var temcode = tablepar.zdydata[0].wigcode;
                        var path = tablepar.zdydata[0].wigurl;
                        pro.$options.components[temcode] = httpVueLoader(path);
                        pro.zydwiget = temcode;
                        isinitQ = true;
                    }
                    if (tablepar.mrcxdata) {
                        //处理默认分类
                        _.forEach(tablepar.mrcxdata, function (obj) {
                            var temp = [];
                            if (obj && obj.datatype != "1") {
                                _.forEach(obj.cxdata.split(','), function (cx) {
                                    temp.push({ issel: false, itemtext: cx })
                                })
                                if (temp.length > 0) {
                                    temp[0].issel = true;
                                }
                                obj.lbdata = temp;
                            }
                        })
                        pro.mrcxdata = tablepar.mrcxdata;
                        pro.tableop.dtcxdata = pro.dtcxdata;
                        if (!isinitQ) {
                            pro.Query();
                            isinitQ = true;
                            //处理默认分类操作
                        }

                    }
                    if (!isinitQ) {
                        pro.Query();
                        //处理默认分类操作
                    }

                } catch (e) {

                }
            },
        },
        mounted: function () {
            var pro = this;
            pro.$nextTick(function () {

                pro.InitWigetData();
            })

        }
    };
</script>